package phoenix.core.helpers;

import phoenix.core.PhoenixManager;
import phoenix.core.base.HelperBase;
import phoenix.web.page_objects.*;
import phoenix.web.page_objects.features.*;

/**
 * Class provides methods for access to page objects (for Web sites)
 */
public class PageWebHelper extends HelperBase
{
	// Page objects - sites
	private IndexPage indexPage;
	private MyAccountPage myAccountPage;
	private FunnelPage funnelPage;
	private SearchPage searchPage;
	private PaymentPage paymentPage;
	private UserPage userPage;
	private MyProfilePage myProfilePage;
	private ActivityPage activityPage;
	private WelcomePage welcomePage;
	private InboxPage inboxPage;
	private MenuPage menuPage;
	private BlockedPage blockedPage;
	private ContactUsPage contactUsPage;
	private ChatRoomsPage chatRoomsPage;
	private ForumPage forumPage;
	// features
	private GalleryPage galleryPage;
	private NaughtyModePage naughtyPage;

	// Page components
	private MessengerPage messenger;

	public PageWebHelper(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	/**
	 * Index page instance
	 * @return IndexPage instance
	 */
	public IndexPage indexPage()
	{
		if (indexPage == null)
		{
			indexPage = new IndexPage(phoenix);
		}

		return indexPage;
	}

	/**
	 * My Account page instance
	 * @return MyAccountPage instance
	 */
	public MyAccountPage myAccountPage()
	{
		if (myAccountPage == null)
		{
			myAccountPage = new MyAccountPage(phoenix);
		}
		return myAccountPage;
	}

	/**
	 * My Profile page instance
	 * @return MyProfilePage instance
	 */
	public MyProfilePage myProfilePage()
	{
		if (myProfilePage == null)
		{
			myProfilePage = new MyProfilePage(phoenix);
		}
		return myProfilePage;
	}

	/**
	 * Funnel page instance
	 * @return FunnelPage instance
	 */
	public FunnelPage funnelPage()
	{
		if (funnelPage == null)
		{
			funnelPage = new FunnelPage(phoenix);
		}

		return funnelPage;
	}

	/**
	 * Payment page instance
	 * @return PaymentPage instance
	 */
	public PaymentPage paymentPage()
	{
		if (paymentPage == null)
		{
			paymentPage = new PaymentPage(phoenix);
		}

		return paymentPage;
	}

	/**
	 * User page instance
	 * @return UserPage instance
	 */
	public UserPage userPage()
	{
		if (userPage == null)
		{
			userPage = new UserPage(phoenix);
		}

		return userPage;
	}

	/**
	 * Search page instance
	 * @return SearchPage instance
	 */
	public SearchPage searchPage()
	{
		if (searchPage == null)
		{
			searchPage = new SearchPage(phoenix);
		}

		return searchPage;
	}

	/**
	 * Activity page instance
	 * @return SearchPage instance
	 */
	public ActivityPage activityPage()
	{
		if (activityPage == null)
		{
			activityPage = new ActivityPage(phoenix);
		}

		return activityPage;
	}

	public MessengerPage messenger()
	{
		if (messenger == null)
		{
			messenger = new MessengerPage(phoenix);
		}
		return messenger;
	}

	/**
	 * Make new session
	 */
	public void closeSession()
	{
		phoenix.wd().openPage("/");
		phoenix.setCountryCookie(testModel().getLocation());
		phoenix.wd().waitForVisibility(indexPage().elements().blockRegForm, "Registration form doesn't appear");
	}

	/**
	 * Logout from a site
	 */
	public void logout()
	{
		if (phoenix.wd().isElementDisplayed(searchPage().elements().blockUserMenu))
		{
			log().info("Logout from the site");
			phoenix.wd().openPage("/site/logout");
			phoenix.wd().waitForVisibility(indexPage().elements().blockRegForm, "Registration form doesn't appear after logout from site");
		}
		else
		{
			log().info("Block 'User menu' doesn't display, may be already test user logout from site");
		}
	}

	public void login(String p_pageURL)
	{
		if (!phoenix.wd().isElementDisplayed(searchPage().elements().btnLogo))
		{
			log().info("Login to the site");
			phoenix.wd().openURL(phoenix.testUser().getAutologin());
			phoenix.wd().openPage(p_pageURL);
		}
	}

	/**
	 * Login to a site
	 */
	public void login()
	{
		login("");
	}

	public WelcomePage welcomePage()
	{
		if (welcomePage == null)
		{
			welcomePage = new WelcomePage(phoenix);
		}

		return welcomePage;
	}

	public InboxPage inboxPage()
	{
		if (inboxPage == null)
		{
			inboxPage = new InboxPage(phoenix);
		}

		return inboxPage;
	}

	public MenuPage menuPage()
	{
		if (menuPage == null)
		{
			menuPage = new MenuPage(phoenix);
		}
		return menuPage;
	}

	public BlockedPage blockedPage()
	{
		if (blockedPage == null)
		{
			blockedPage = new BlockedPage(phoenix);
		}
		return blockedPage;
	}

	public ContactUsPage contactUsPage()
	{
		if (contactUsPage == null)
		{
			contactUsPage = new ContactUsPage(phoenix);
		}
		return contactUsPage;
	}

	public ChatRoomsPage chatRoomsPage()
	{
		if (chatRoomsPage == null)
		{
			chatRoomsPage = new ChatRoomsPage(phoenix);
		}
		return chatRoomsPage;
	}

	public ForumPage forumPage()
	{
		if (forumPage == null)
		{
			forumPage = new ForumPage(phoenix);
		}
		return forumPage;
	}

	public GalleryPage galleryPage()
	{
		if (galleryPage == null)
		{
			galleryPage = new GalleryPage(phoenix);
		}
		return galleryPage;
	}

	public NaughtyModePage naughtyModePage()
	{
		if (naughtyPage == null)
		{
			naughtyPage = new NaughtyModePage(phoenix);
		}
		return naughtyPage;
	}
}
